/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Army;

import Army.building.Building;
import Army.unit.Unit;
import MOM.Message;
import java.awt.Point;

/**
 *
 * @author Huy
 */
public class ComputerTeam extends PlayerTeam {

	private Group workerGroup;
	private Group archerGroup;
	private Group swordGroup;
	private Group spearGroup;
	private int nMine = 0;
	private int nBarrack = 0;
	private int nWorker = 0;
	private Building calledBuilding;
	private int attackTime = 0;
	private int deadTime = 0;
	private Point deadPosition;
	private int seriouslyPoint = 0;

	public Building getCalledBuilding() {
		return calledBuilding;
	}

	public Point getDeadPosition() {
		return deadPosition;
	}

	public int getSeriouslyPoint() {
		return seriouslyPoint;
	}

	@Override
	public boolean isIsControled() {
		return false;
	}

	public int getnMine() {
		return nMine;
	}

	public int getnBarrack() {
		return nBarrack;
	}

	public int getnWorker() {
		return nWorker;
	}

	public Group getWorkerGroup() {
		return workerGroup;
	}

	public Group getArcherGroup() {
		return archerGroup;
	}

	public Group getSwordGroup() {
		return swordGroup;
	}

	public Group getSpearGroup() {
		return spearGroup;
	}

	public ComputerTeam(int team, int x, int y) {
		super(team, x, y);
		workerGroup = new Group(false);
		archerGroup = new Group(false);
		swordGroup = new Group(false);
		spearGroup = new Group(false);
	}

	@Override
	public boolean add(Building building) {
		if (building == null || gold < building.getBuildCost() || getPopulation() > populationLimit) {
			return false;
		}
		gold -= building.getBuildCost();
		listBuilding.add(building);
		switch (building.getType()) {
			case BuildingMine:
				nMine++;
				break;
			case BuildingBarrack:
				nBarrack++;
				break;
		}
		return true;
	}

	@Override
	public Unit createNewUnit(String type, Building b) {
		if ("worker".equals(type)) {
			nWorker++;
		}
		return super.createNewUnit(type, b);

	}

	@Override
	protected Unit removeUnit(int i) {
		Unit unit = super.removeUnit(i);
		switch (unit.getType()) {
			case UnitWorker:
				//nWorker--;
				workerGroup.getListUnit().remove(unit);
				nWorker = workerGroup.size();
				break;
			case UnitArcher:
				archerGroup.getListUnit().remove(unit);
				break;
			case UnitSpear:
				spearGroup.getListUnit().remove(unit);
				break;
			case UnitSword:
				swordGroup.getListUnit().remove(unit);
				break;
		}
		if (deadPosition == null) {
			deadTime = 300;
			deadPosition = unit.getPosition();
			seriouslyPoint++;
		} else if (unit.getPosition().distance(deadPosition) < 500) {
			deadTime = 300;
			seriouslyPoint++;
			deadPosition.x = (unit.getPosition().x + deadPosition.x) / 2;
			deadPosition.y = (unit.getPosition().y + deadPosition.y) / 2;
		}
		return unit;
	}

	@Override
	protected Building removeBuilding(int i) {
		Building building = super.removeBuilding(i);
		switch (building.getType()) {
			case BuildingBarrack:
				nBarrack--;
				break;
			case BuildingMine:
				nMine--;
				break;
		}
		return building;
	}

	/*	@Override
	 protected void remove(BuildingAndUnit unit) {
	 if (unit.isUnit()) {
	 listUnit.remove(unit);
	 switch (unit.getType()) {
	 case UnitWorker:
	 nWorker--;
	 workerGroup.getListUnit().remove(unit);
	 break;
	 case UnitArcher:
	 archerGroup.getListUnit().remove(unit);
	 break;
	 case UnitSpear:
	 spearGroup.getListUnit().remove(unit);
	 break;
	 case UnitSword:
	 swordGroup.getListUnit().remove(unit);
	 break;
	 }
	 } else {
	 listBuilding.remove(unit);
	 switch (unit.getType()) {
	 case BuildingBarrack:
	 nBarrack--;
	 break;
	 case BuildingMine:
	 nMine--;
	 break;
	 }
	 }
	 if (deadPosition == null) {
	 deadTime = 300;
	 deadPosition = unit.getPosition();
	 seriouslyPoint++;
	 } else if (unit.getPosition().distance(deadPosition) < 500) {
	 deadTime = 300;
	 seriouslyPoint++;
	 deadPosition.x = (unit.getPosition().x + deadPosition.x) / 2;
	 deadPosition.y = (unit.getPosition().y + deadPosition.y) / 2;
	 }
	 }*/
	@Override
	public void notify(Message msg) {
		/*if (!isAlive()) {
		 return;
		 }*/
		if (msg.message.equalsIgnoreCase("attacked")) {
			if (calledBuilding == null
					|| (calledBuilding != null && calledBuilding.getBuildCost() < ((Building) msg.object).getBuildCost())) {
				calledBuilding = (Building) msg.object;
				attackTime = 500;
			} else if (calledBuilding == (Building) msg.object) {
				attackTime = 500;
			}
		} else {
			super.notify(msg);
		}
	}

	@Override
	public void update() {
		super.update();
		if (attackTime > 0) {
			attackTime--;
		} else {
			calledBuilding = null;
		}
		if (deadTime > 0) {
			deadTime--;
		} else {
			seriouslyPoint = 0;
			deadPosition = null;
		}

	}

	@Override
	protected boolean add(Unit unit) {
		listUnit.add(unit);
		switch (unit.getType()) {
			case UnitWorker:
				workerGroup.add(unit);
				break;
			case UnitArcher:
				archerGroup.add(unit);
				break;
			case UnitSword:
				swordGroup.add(unit);
				break;
			case UnitSpear:
				spearGroup.add(unit);
				break;
		}
		return true;
	}
}
